package com.solution.pc.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.solution.admin.auth.service.UserService;
import com.solution.admin.customer.service.CustomerLevelService;
import com.solution.admin.customer.service.CustomerService;
import com.solution.admin.home.service.SiteConfigItemService;
import com.solution.admin.order.constants.OrderConstants;
import com.solution.admin.order.entity.OrderDetailEntity;
import com.solution.admin.order.entity.OrderEntity;
import com.solution.admin.order.service.OrderDetailService;
import com.solution.admin.order.service.OrderService;
import com.solution.admin.product.entity.ProductEntity;
import com.solution.admin.product.entity.ProductSpecEntity;
import com.solution.admin.product.service.ProductPriceService;
import com.solution.admin.product.service.ProductService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.pc.order.entity.OrderEvaluationEntity;
import com.solution.pc.order.service.OrderEvaluationService;
import com.solution.wx.my.service.UserFavoriteService;

/**
 * 订单商品评价web控制层
 * @author xkl
 * @date 2017-11-28 11:42:28
 */
@RequestMapping("/pc/evaluation")
@Controller
public class PcEvaluationController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(PcEvaluationController.class);

	@Autowired
	private OrderEvaluationService orderEvaluationService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderDetailService orderDetailService;

	@Autowired
	private UserService userService;

	@Autowired
	private CustomerLevelService customerLevelService;
	
	@Autowired
	private SiteConfigItemService siteConfigItemService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductPriceService productPriceService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private UserFavoriteService userFavoriteService;

	/**
	 * 跳转至订单评价页面
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年11月28日 上午10:30:50
	 */
	@RequestMapping("/toOrderEvaluation")
	public String toOrderEvaluation(HttpServletRequest request) {
		String orderCode = request.getParameter("orderCode");
		OrderEntity order = orderService.getOrderByCode(orderCode);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		List<OrderDetailEntity> orderDetailList = orderDetailService.listOrderDetail(paramMap);
		for (OrderDetailEntity detailEntity : orderDetailList) {
			List<ProductSpecEntity> productSpecList = JSON.parseArray(detailEntity.getProductSpec(), ProductSpecEntity.class);
			detailEntity.setProductSpecList(productSpecList);
		}

		//只能进入自己的订单
		if (null == order || order.getCustomerId() != getCustomerId()) {
			return "redirect:/pc/index";
		}

		request.setAttribute("order", order);
		request.setAttribute("orderDetailList", orderDetailList);
		return "pc/my/order_evaluation";
	}

	/**
	 * 提交评论
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年11月29日 上午10:49:36
	 */
	@RequestMapping("/commitEvaluation")
	@ResponseBody
	public Map<String, Object> commitEvaluation(HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String orderCode = request.getParameter("orderCode");
		String evaluationJson = request.getParameter("evaluationList");
		JSONArray array = JSONArray.parseArray(evaluationJson);
		OrderEntity order = orderService.getOrderByCode(orderCode);
		//只能评价自己的已完成订单
		if (null == order || order.getCustomerId() != getCustomerId() || !OrderConstants.ORDER_STATUS.FINISHED.getCode().equals(order.getOrderStatus())) {
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
			return resultMap;
		}
		//只能评价未评价订单
		if ("1".equals(order.getAppraiseFlag())) {
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "该订单已评价完成");
			return resultMap;
		}
		List<OrderEvaluationEntity> evaluationList = new ArrayList<OrderEvaluationEntity>();
		for (int i = 0; i < array.size(); i++) {
			JSONObject object = array.getJSONObject(i);
			OrderEvaluationEntity evaluation = new OrderEvaluationEntity();
			evaluation.setOrderId(order.getId());
			evaluation.setOrderCode(orderCode);
			evaluation.setProductCode(object.getString("productCode"));
			evaluation.setProductName(object.getString("productName"));
			evaluation.setImagePath(object.getString("imagePath"));
			evaluation.setScore(object.getInteger("score"));
			evaluation.setContent(object.getString("content"));
			evaluation.setFilePath(object.getString("filePath"));
			evaluation.setUserId(getCustomerId());
			evaluation.setUserName(getCustomer().getUserName());
			evaluation.setCreater(getCustomer().getUserName());
			evaluation.setHeadImage(getCustomer().getHeadImage());
			evaluation.setLevelName(getCustomerLevel());
			evaluation.setChannel(OrderConstants.ORDER_CHANNEL.PC.getCode());;
			evaluationList.add(evaluation);
		}
		int result = orderEvaluationService.saveOrderEvaluation(evaluationList, order.getId());
		resultMap.put(PublicStatus.RESULT_CODE, result>0?"1":"0");
		resultMap.put(PublicStatus.RESULT_MSG, result>0?"操作成功":"操作失败");
		return resultMap;
	}

	/**
	 * 跳转至评价完成界面
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年11月29日 下午2:20:45
	 */
	@RequestMapping("/toEvaluationResult")
	public String toEvaluationResult(HttpServletRequest request) {
		//我的收藏
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userCode", getUserCode());
		paramMap.put("isLogin", 1);
		paramMap.put("customerLevel", getCustomerLevel());
		paramMap.put("customerId", getCustomerId());
		List<ProductEntity> favoriteList = productService.listUserFavoriteProduct(paramMap);
		userFavoriteService.formatProductPrice(favoriteList);
		request.setAttribute("favoriteList", favoriteList);
		return "pc/my/evaluation_result";
	}

}
